package cn.edu.scau.cmi.liweihang.spring.hibernate.dao;

import java.util.List;

import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import cn.edu.scau.cmi.liweihang.spring.hibernate.dao.BaseHibernateDAO;
import cn.edu.scau.cmi.liweihang.spring.hibernate.dao.StudentDAO;
import cn.edu.scau.cmi.liweihang.spring.hibernate.domain.Student;
import cn.edu.scau.cmi.liweihang.spring.hibernate.domain.Teacher;

public class TeacherDAO extends BaseHibernateDAO {
	private static final Logger log = LoggerFactory.getLogger(TeacherDAO.class);
	// property constants
	public static final String NAME = "name";
	StudentDAO sd = new StudentDAO();
	private Session session=getSession();

	/**
	 * 添加一个老师
	 * 
	 * @param transientInstance
	 */
	public void save(Teacher transientInstance) {
		log.debug("saving Teacher instance");
		try {
			getSession().beginTransaction();
			getSession().save(transientInstance);
			log.debug("save successful");
			getSession().getTransaction().commit();;
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/**
	 * 删除一个老师
	 * 
	 * @param persistentInstance
	 */
	public void delete(Teacher persistentInstance) {
		log.debug("deleting Teacher instance");
		try {
			session.beginTransaction();
			session.clear();
			session.delete(persistentInstance);
			log.debug("delete successful");
			session.getTransaction().commit();
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/**
	 * 修改老师的姓名
	 * 
	 * @param name
	 * @param updateId
	 * @return
	 */
	public Teacher updateName(String name, Long updateId) {
		log.debug("updating Teacher instance");
		try {
			session.beginTransaction();
			session.clear();
			Teacher teacher = (Teacher) session.get(Teacher.class, updateId);
			teacher.setName(name);
			session.getTransaction().commit();
			return teacher;
		} catch (Exception e) {

			log.error("update failed,", e);
			throw e;
		}
	}

	/**
	 * 通过id查找老师
	 * 
	 * @param id
	 * @return
	 */
	public Teacher findById(java.lang.Long id) {
		log.debug("getting Teacher instance with id: " + id);
		try {
			Teacher instance = (Teacher) getSession().get(Teacher.class, id);			
				return instance;						
		} catch (RuntimeException re) {
			return null;
		}
	}

	
	public void update(Teacher instance) {
		log.debug("update a teacher instance");
		try {
			getSession().update(instance);
			log.debug("update successful");
		}catch(RuntimeException re) {
			log.error("update failed",re);
			throw re;
		}
	}
	
	public List<?> findByProperty(String propertyName, Object value) {
		log.debug("finding Teacher instance with property: " + propertyName + ", value: " + value);
		try {
			String queryString = "from Teacher as model where model." + propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/**
	 * 查找所有老师
	 * 
	 * @return
	 */
	public List<?> findAll() {
		log.debug("finding all Teacher instances");
		try {
			String queryString = "from Teacher";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/**
	 * 教师选择被指导的学生
	 */
	public Student updateTutor(Long teacherId, Long studentId) {
		log.debug("updating Student instance");
		try {
			getSession().beginTransaction();
			Student student = (Student) getSession().get(Student.class, studentId);
			Teacher tutor = (Teacher) getSession().get(Teacher.class, teacherId);
			student.setTeacher(tutor);
			getSession().getTransaction().commit();
			return student;
		} catch (Exception e) {
			// TODO: handle exception
			log.error("update failed,", e);
			throw e;
		}
	}

}